package com.flower.enjoyment.flower.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flower.enjoyment.flower.entity.FlowerCardOrder;
import com.flower.enjoyment.flower.entity.PresentDetail;
import com.flower.enjoyment.flower.entity.UserInfo;
import com.flower.enjoyment.flower.entityDo.FlowerCardDo;
import com.flower.enjoyment.flower.entityDo.SysPayDo;
import com.flower.enjoyment.flower.entityDo.UserInfoDo;
import com.flower.enjoyment.flower.mapper.FlowerCardOrderMapper;
import com.flower.enjoyment.flower.mapper.PresentDetailMapper;
import com.flower.enjoyment.flower.mapper.SysPayMapper;
import com.flower.enjoyment.flower.mapper.UserInfoMapper;
import com.flower.enjoyment.flower.service.FlowersCardOrderService;
import com.flower.enjoyment.flower.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FlowersCardOrderServiceImpl implements FlowersCardOrderService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private FlowerCardOrderMapper flowerCardOrderMapper;
    @Autowired
    private SysPayMapper sysPayMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private PresentDetailMapper presentDetailMapper;

    /**
     * 花享卡下单
     *
     * @param flowerCardOrder
     * @return
     */
    @Override
    @Transactional
    public int insertCardOrder(FlowerCardOrder flowerCardOrder,String payId) {
       SysPayDo sysPay = sysPayMapper.selectByPrimaryKey(payId);
         /*if (sysPay.getIspresent() ==1){
            flowerCardOrder.setAmount(Double.valueOf(sysPay.getMoney()-sysPay.getIsShareMoney()));
        }else {
            flowerCardOrder.setAmount(Double.valueOf(sysPay.getMoney()));
        }*/
        flowerCardOrder.setAmount(Double.valueOf(sysPay.getMoney()));
        flowerCardOrder.setSysPayId(sysPay.getId());
        return flowerCardOrderMapper.insert(flowerCardOrder);
    }

    /**
     * 根据ID获取订单详情
     *
     * @param id
     * @return
     */
    @Override
    public FlowerCardOrder selectCardOrderInfo(String id) {
        return flowerCardOrderMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据订单号获取订单信息
     *
     * @param no
     * @return
     */
    @Override
    public FlowerCardOrder selectCardOrderByNo(String no) {
        return flowerCardOrderMapper.selectCardOrderByNo(no);
    }

    /**
     * 修改花享卡订单信息
     *
     * @param flowerCardOrder
     * @return
     */
    @Override
    @Transactional
    public int updateCardOrder(FlowerCardOrder flowerCardOrder) {
        //查询用户信息
        UserInfoDo userInfoDo = userInfoMapper.selectByPrimaryKey(flowerCardOrder.getUserId());
        logger.info("根据下单人ID查询下单人信息：{}", JSONObject.toJSONString(userInfoDo));
        if (userInfoDo.getSource()==1 && userInfoDo.getFirstCardPay() == 0){
            logger.info("该用户被分享过来的：{}，是第一次购买花享卡",userInfoDo.getId());
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userInfoDo.getId());
            userInfo.setFirstCardPay(1);
            SysPayDo sysPayDo = sysPayMapper.selectByPrimaryKey(flowerCardOrder.getSysPayId());
            userInfo.setAmount(sysPayDo.getMoney()+userInfoDo.getAmount());
            if (sysPayDo.getIspresent() == 1) {
                userInfo.setGiveAmount(sysPayDo.getPresentMoney() + userInfo.getGiveAmount());
            }
            if (sysPayDo.getIsShareMoney()>0) {
                logger.info("赠送分享人金额开始");
                PresentDetail presentDetail = new PresentDetail();
                presentDetail.setId(RandomUtil.getUuid());
                presentDetail.setAmount(sysPayDo.getIsShareMoney());
                presentDetail.setServiceName("分享获取");
                presentDetail.setUserId(userInfoDo.getSourceUserId());
                presentDetail.setCardId(flowerCardOrder.getCardId());
                presentDetailMapper.insert(presentDetail);
                //获取分享人信息
                UserInfoDo userDo = userInfoMapper.selectByPrimaryKey(userInfoDo.getSourceUserId());
                UserInfo user = new UserInfo();
                user.setId(userInfoDo.getSourceUserId());
                user.setGiveAmount(userDo.getGiveAmount()+sysPayDo.getIsShareMoney());

                logger.info("赠送分享人金额结束");
            }
            if (sysPayDo.getBeShareMoney() > 0){
                logger.info("赠送被分享人金额开始");
                PresentDetail presentDetail = new PresentDetail();
                presentDetail.setId(RandomUtil.getUuid());
                presentDetail.setAmount(sysPayDo.getBeShareMoney());
                presentDetail.setServiceName("分享获取");
                presentDetail.setUserId(userInfoDo.getId());
                presentDetail.setBalance(sysPayDo.getPresentMoney().doubleValue());
                presentDetail.setCardId(flowerCardOrder.getCardId());
                presentDetailMapper.insert(presentDetail);
                userInfo.setGiveAmount(sysPayDo.getBeShareMoney() + userInfoDo.getGiveAmount());
                logger.info("赠送被分享人金额结束");
            }
            userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }else{

            UserInfo userInfo = new UserInfo();
            userInfo.setId(userInfoDo.getId());
            SysPayDo sysPayDo = sysPayMapper.selectByPrimaryKey(flowerCardOrder.getSysPayId());
            logger.info("根据支付金额ID后去金额信息：{}",JSONObject.toJSONString(sysPayDo));
            userInfo.setAmount(sysPayDo.getMoney()+userInfoDo.getAmount());
            userInfo.setFirstCardPay(1);
            if (sysPayDo.getIspresent() == 1) {
                userInfo.setGiveAmount(sysPayDo.getPresentMoney() + userInfoDo.getGiveAmount());
                PresentDetail presentDetail = new PresentDetail();
                presentDetail.setId(RandomUtil.getUuid());
                presentDetail.setAmount(sysPayDo.getPresentMoney().doubleValue());
                presentDetail.setServiceName("购卡赠送金额");
                presentDetail.setCardId(flowerCardOrder.getCardId());
                presentDetail.setUserId(userInfoDo.getId());
                presentDetail.setBalance(sysPayDo.getPresentMoney().doubleValue());
                presentDetailMapper.insert(presentDetail);
            }
            userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }
        return flowerCardOrderMapper.updateByPrimaryKeySelective(flowerCardOrder);
    }

    /**
     * 将订单置为过期
     *
     * @return
     */
    @Override
    public int updateExpireCardOrder() {
        return flowerCardOrderMapper.updateExpireCardOrder();
    }

    @Override
    public List<FlowerCardDo> selectByUserId(String userId) {
        return flowerCardOrderMapper.selectByUserId(userId);
    }
}
